package com.zzzyh.forumsystemclient.cotroller;

import com.alibaba.fastjson.JSON;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.Article;
import com.zzzyh.forumsystemclient.model.Feedback;
import com.zzzyh.forumsystemclient.model.LoginUser;
import com.zzzyh.forumsystemclient.model.PageResult;
import com.zzzyh.forumsystemclient.services.IFeedbackService;
import com.zzzyh.forumsystemclient.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/feedback")
public class FeedbackController {
    @Autowired
    private IFeedbackService feedbackService;
    @RequestMapping("/add")
    public AppResult<String> add(@RequestBody Feedback feedback,
                                 @RequestHeader("Authorization") String token){
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        feedback.setUserId(user.getUser().getId());
        feedback.setCreateTime(new Date());
        feedbackService.add(feedback);
        return AppResult.success("添加成功");
    }
    @RequestMapping("/list")
    public AppResult<PageResult<Feedback>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                                @RequestParam(required = false) String title,
                                                @RequestParam(required = false) String source,
                                                @RequestHeader("Authorization") String token){
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        List<Feedback> feedbackList = feedbackService.getFeedbackList(pageNum, pageSize, user.getUser().getId());
        Integer total = feedbackService.total(user.getUser().getId());
        return AppResult.success(new PageResult<>(pageNum, pageSize, total, feedbackList));
    }
}
